unit dlgAuthForm;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, ActnList, ToolWin, ActnMan, ActnCtrls,
  XPStyleActnCtrls, ImgList, ComCtrls, StdStyleActnCtrls, Buttons,
  User;

type
  TAuthForm = class(TForm)
    pnlAuthForm: TPanel;
    ActionList1: TActionList;
    actUserAdd: TAction;
    actUserEdit: TAction;
    actUserDelete: TAction;
    Panel1: TPanel;
    BitBtn1: TBitBtn;
    BitBtn2: TBitBtn;
    BitBtn3: TBitBtn;
    Panel2: TPanel;
    ListView1: TListView;
    pnlDirectory: TPanel;
    procedure actUserAddExecute(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure actUserEditExecute(Sender: TObject);
    procedure actUserDeleteExecute(Sender: TObject);
    procedure ActionList1Update(Action: TBasicAction;
      var Handled: Boolean);
    procedure ListView1DblClick(Sender: TObject);
  private
    FUsers: TUsers;
  public
  end;

implementation

uses
  dlgUser;

{$R *.dfm}

procedure TAuthForm.actUserAddExecute(Sender: TObject);
var
  lUser: TUser;
begin
  lUser := TUser.Create(nil);
  if TfrmUser.Execute(lUser, acAdd) then
  begin
    lUser.Collection := FUsers;
    with ListView1.Items.Add do
    begin
      Data := lUser;
      Caption := lUser.UserName;
      if lUser.Enabled then
        SubItems.Add('True')
      else
        SubItems.Add('False');
      FUsers.SaveToFile;
    end;
  end
  else
    lUser.Free;
end;

procedure TAuthForm.FormCreate(Sender: TObject);
var
  I: Integer;
begin
  FUsers := TUsers.Create;
  FUsers.LoadFromFile;
  for I := 0 to FUsers.Count - 1 do
  begin
    with ListView1.Items.Add do
    begin
      Data := FUsers[I];
      Caption := FUsers[I].UserName;
      if FUsers[I].Enabled then
        SubItems.Add('True')
      else
        SubItems.Add('False');
    end;
  end;
end;

procedure TAuthForm.FormDestroy(Sender: TObject);
begin
  FUsers.Free;
end;

procedure TAuthForm.actUserEditExecute(Sender: TObject);
var
  lUser: TUser;
begin
  lUser := TUser(ListView1.Selected.Data);
  if TfrmUser.Execute(lUser, acEdit) then
  begin
    lUser.Collection := FUsers;
    with ListView1.Selected do
    begin
      Caption := lUser.UserName;
      if lUser.Enabled then
        SubItems[0] := 'True'
      else
        SubItems[0] := 'False';
      FUsers.SaveToFile;
    end;
  end;
end;

procedure TAuthForm.actUserDeleteExecute(Sender: TObject);
var
  lUser: TUser;
begin
  if MessageDlg('Do you want to delete this user?', mtConfirmation, [mbYes, mbNo], 0) = mrYes then
  begin
    lUser := TUser(ListView1.Selected.Data);
    ListView1.Items.Delete(FUsers.FindItemID(lUser.ID).Index);
    FUsers.SaveToFile;
  end;
end;

procedure TAuthForm.ActionList1Update(Action: TBasicAction;
  var Handled: Boolean);
var
  lEnabled: Boolean;
begin
  lEnabled := (ListView1.Selected <> nil);
  actUserEdit.Enabled := lEnabled;
  actUserDelete.Enabled := lEnabled;
end;

procedure TAuthForm.ListView1DblClick(Sender: TObject);
begin
  actUserEdit.Execute;
end;

end.
